#include "serial.h"
#include <QDebug>

serial::serial()
{
    // 定义文本编码
    tc = QTextCodec::codecForName("UTF-8");
}

serial::~serial()
{
    m_serialPost.clear();
    m_serialDescription.clear();
    m_serialData.clear();
}

void serial::setSerialPort(QString port)
{
    this->m_serialPost.append(port);
}

QStringList serial::getSerialPort()
{
    return this->m_serialPost;
}

void serial::clearSerialPort()
{
    this->m_serialPost.clear();
}

void serial::setSerialDescription(QString description)
{
    this->m_serialDescription.append(description);
}

QStringList serial::getSerialDescription()
{
    return this->m_serialDescription;
}

void serial::clearSerialDescription()
{
    this->m_serialDescription.clear();
}

void serial::setSerialData(QByteArray data)
{
    this->m_serialData = data;
}

QByteArray serial::getSerialData()
{
    return this->m_serialData;
}

void serial::clearSerialData()
{
    this->m_serialData.clear();
}

void serial::setTxBytes(int txBytes)
{
    this->m_txBytes = txBytes;
}

int serial::getTxBytes()
{
    return this->m_txBytes;
}

void serial::setRxBytes(int rxBytes)
{
    this->m_rxBytes = rxBytes;
}

int serial::getRxBytes()
{
    return this->m_rxBytes;
}

void serial::setSerialOnOff(bool OnOff)
{
    this->m_serialOnOff = OnOff;
}

bool serial::getSerialOnOff()
{
    return this->m_serialOnOff;
}

QString serial::byteArray2String(QByteArray byteArray)
{
    return tc->toUnicode(byteArray);
}

QByteArray serial::string2ByteArray(QString string)
{
    return tc->fromUnicode(string);
}

void serial::serialScan(void)
{
    clearSerialPort();
    clearSerialDescription();
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        this->setPort(info);
        setSerialPort(this->portName());
        setSerialDescription(info.description());
//        qDebug() << this->portName();
//        qDebug() << info.description();
    }
    // 无串口连接时 默认None
    if(getSerialPort().length() <= 0) setSerialPort("None");
    if(getSerialDescription().length() <= 0) setSerialDescription("None");
    emit serialScanCompleted();
}

bool serial::serialOpen(QString serialName, int baudRate)
{
    this->setPortName(serialName);
    if(this->open(QIODeviceBase::ReadWrite)) {
        this->setBaudRate(baudRate);
        this->setDataBits(QSerialPort::Data8);
        this->setParity(QSerialPort::NoParity);
        this->setStopBits(QSerialPort::OneStop);
        this->setFlowControl(QSerialPort::NoFlowControl);
        // 下位机发送数据会响应这个槽函数
        connect(this, &QSerialPort::readyRead, this, &serial::readSerialData);
        setSerialOnOff(true);
        emit serialOpened();
        return true;
    }
    setSerialOnOff(false);
    emit serialOpenFailed();
    return false;
}

void serial::readSerialData()
{
    setSerialData(this->readAll());
    setRxBytes(getRxBytes() + tc->toUnicode(getSerialData()).length());
    emit this->readDataCompleted(getRxBytes());
}

bool serial::sendSerialData(QByteArray sendData)
{
    int txBytes;
    txBytes = this->write(sendData);
    // 发送失败返回-1
    if(txBytes != -1)
    {
        setTxBytes(getTxBytes() + txBytes);
        emit this->sendDataCompleted(getTxBytes());
        return true;
    }
    return false;
}

void serial::serialClose()
{
    this->clear();
    this->close();
    setSerialOnOff(false);
    emit serialClosed();
}
